home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2762 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  99 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Assigning function pointer in C/C++.
  5. Date: 19 Jan 1996 16:35:14 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19113514@g7240065.bridge.bst.bls.com>
  8. References: <DL3JJu.5nB.0.queen@torfree.net> <4doc42$gsb@bmdhh222.bnr.ca>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Ray Fallon's message of 19 Jan 1996 15:07:46 GMT
  11.  
  12. In article <4doc42$gsb@bmdhh222.bnr.ca> Ray Fallon <ray.fallon@nt.com> writes:
  13.  
  14. : bh332@freenet.toronto.on.ca (Karim Ladha) wrote:
  15. :> 
  16. :> 
  17. :> How is it possible to assign a declared variable in C++ a pointer to
  18. :> some function member? If you know of a solution, post. Greatly appreciated.
  19. :> 
  20. :> In C for example;
  21. :> ...
  22. :> void( far *MyFunc )();
  23. :> ...
  24.  
  25. : I response to your question, assigment of pointers to member functions is
  26. : not possible for the following reason : 
  27.  
  28. : In 'C' you can only have one instance of a function at any one time. In 
  29. : C++ you can have multiple instances of a class (i.e. objects) and the
  30. : pointer to the function would not be able to know which object you are
  31. : referring to.
  32. <snip>
  33.  
  34. Huh?
  35.  
  36. You can have pointers to member functions providing you know what class (or base
  37. class) you are going to be assigning functions from.
  38.  
  39.    int (A::*p)(void);
  40.  
  41. declares p as a pointer to an A member function which takes nothing and returns
  42. an int. The usage of p requires an object or pointer to an object of
  43. type A or derived from A. i.e:
  44.   
  45.    A a;
  46.    int z = (a.*p)();    // Uses '.*' operator.
  47. or
  48.    A* a = new B;    // Where 'B' is derived from 'A'
  49.    int z = (a->*p)();    // Uses '->*' operator.
  50.  
  51. More complete example:
  52.  
  53.   #include <iostream.h>
  54.  
  55.   class A
  56.   {
  57.     public:
  58.       virtual int func(void)
  59.       { return 23; }
  60.  
  61.       int fred(void)
  62.       { return 7; }
  63.   };
  64.  
  65.   class B : public A
  66.   {
  67.     public:
  68.       virtual int func(void)
  69.       { return 71; }
  70.  
  71.       int fred(void)
  72.       { return 24; }
  73.   };
  74.  
  75.   int
  76.   main(void)
  77.   {
  78.       int (A::*p)(void) = A::func;
  79.       A a;
  80.       A* b = new B;
  81.  
  82.       cout << (a.*p)() << endl;        // Prints 23
  83.       cout << (b->*p)() << endl;    // Prints 71 (func is virtual)
  84.  
  85.       p = A::fred;
  86.  
  87.       cout << (a.*p)() << endl;        // Prints 7
  88.       cout << (b->*p)() << endl;    // Prints 7  (fred is not virtual)
  89.  
  90.       return 0;
  91.   }
  92.  
  93. Hope this helps.
  94. Regards
  95.  
  96.    -A.
  97. -- 
  98. | A.Champion                |
  99.